#!/usr/bin/env python

# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Attribute Info Class """

from streetidentity_rp.error import  DataError

__author__ = "Maciej Machulak"
__maintainer__ = "Maciej Machulak"
__email__ = "mmachulak@google.com"

__copyright__ = "Copyright 2012 Google Inc. All Rights Reserved."
__license__ = "Apache License 2.0"
__version__ = "0.2"
__status__ = "Prototype"


class AttributeInfo(object):
    """Represents a single attribute returned by the attribute endpoint at IDP.
    """

    id_ = None
    type = None
    desc = None
    uri = None
    icon_url = None
    provider_id = None

    def __init__(self, attribute):

        if not isinstance(attribute, dict):
            raise DataError('invalid data format')

        self.id_ = attribute.get('id', None)
        self.type = attribute.get('type', None)
        self.desc = attribute.get('description', None)
        self.uri = attribute.get('uri', None)
        self.icon_url = attribute.get('icon_url', None)
        self.provider_id = attribute.get('provider_id', None)

    def __repr__(self):
        return self.__str__()

    def __str__(self):
        attribute = ''
        attribute += 'id: ' + (self.id_ or '')
        attribute += '\ntype: ' + (self.type or '')
        attribute += '\ndesc: ' + (self.desc or '')
        attribute += '\nuri: ' + (self.uri or '')
        attribute += '\nicon_url: ' + (self.icon_url or '')
        attribute += '\nprovider_id: ' + (self.provider_id or '')
        return attribute


class AuthorizedAttributeInfo(object):
    """Authorized attribute info - contains access token for the AP."""

    attribute_info = None
    access_token = None

    def __init__(self, attribute_info, access_token):
        self.attribute_info = attribute_info
        self.access_token = access_token


class AttributeListInfo(list):
    """Represents the Attribute Info List returned by the Attribute endpoint at
    the IDP."""

    def __init__(self, attribute_list_dict):
        super(AttributeListInfo, self).__init__()
        if not isinstance(attribute_list_dict, dict):
            raise DataError('invalid data format')

        attribute_list = attribute_list_dict.get('attribute_list', None)
        if not attribute_list:
            raise DataError('missing attribute_list name')

        if not hasattr(attribute_list, '__contains__'):
            raise DataError('attribute_list not a list')

        self.attribute_list = []
        for attribute in attribute_list:
            self.attribute_list.append(AttributeInfo(attribute))

    def __len__(self):
        return len(self.attribute_list)

    def __getitem__(self, item):
        return self.attribute_list[item]

    def __contains__(self, item):
        return super(AttributeListInfo, self).__contains__(item)

    def get_by_id(self, attribute_id):
        """
        Returns the AttributeInfo for attribute with a specified ID.
        """
        for attribute in self.attribute_list:
            if attribute.id_ == attribute_id:
                return attribute
        return None
